package com.cli.controller;

import com.cli.service.OcrService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @Author xiongmin
 * @Description
 * @Date 2021/2/2 12:03
 * @Version 1.0
 * @TODO
 **/
@Controller
@Slf4j
public class UploadController {
    @Autowired
    private OcrService ocrService;

    @GetMapping("/")
    public String index() {
        return "upload";
    }


    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file")MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }
        // 打印文件内容
//        readFileContent(file);
        String fileName = file.getOriginalFilename();
        String filePath = "D:\\javawork\\springboot-cli\\orc\\src\\main\\java\\resources\\files\\";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            String content = ocrService.doOCR(dest.getPath());
            System.out.println("content = " + content);
            log.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            log.error(e.toString(), e);
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
        return "上传失败！";
    }

    @PostMapping("/multiUpload")
    @ResponseBody
    public String multiUpload(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        String filePath = "files/";
        for (int i = 0; i < files.size(); i++) {
            MultipartFile file = files.get(i);
            if (file.isEmpty()) {
                return "上传第" + (i++) + "个文件失败";
            }
            String fileName = file.getOriginalFilename();

            File dest = new File(filePath + fileName);
            try {
                file.transferTo(dest);
                log.info("第" + (i + 1) + "个文件上传成功");
            } catch (IOException e) {
                log.error(e.toString(), e);
                return "上传第" + (i++) + "个文件失败";
            }
        }

        return "上传成功";

    }

    private void readFileContent(MultipartFile file) {
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {

            inputStream = file.getInputStream();
//            inputStreamReader = new InputStreamReader(inputStream);
//            bufferedReader = new BufferedReader(inputStreamReader);
//            String lineTxt = null;
//            while (null != (lineTxt = bufferedReader.readLine())) {
//                System.out.println(lineTxt);
//            }


            byte[] bytes = new byte[1024];
            int len = 1024;
            int size = 0;
            while ((size = inputStream.read(bytes)) > 0) {
                String str = new String(bytes,0,size);
                System.out.println(str);
            }
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        } finally {
            try {
                if (null != bufferedReader) {
                    bufferedReader.close();
                }
                if (null != inputStreamReader) {
                    inputStreamReader.close();
                }
                if (null != inputStream) {
                    inputStream.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage(),e);
            }

        }
    }

}
